<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Page extends Admin_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('page_m');
        $this->load->library('pagination');
    }

    /**
     * 列表
     * [index description]
     * @return [type] [description]
     */
    public function index($page = 1)
    {

        $limit                      = 20;
        $config['uri_segment']      = 4;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->db->count_all('pages');
        $config['base_url']         = site_url('manage/page/index/');
        $config['per_page']         = $limit;
        $config['num_links']        = 10;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $pages              = $this->page_m->get_all_pages($start, $limit);
        $data['pages']      = $pages;
        $this->load->view('pages/index', $data);
    }

    /**
     * 新增
     * [add description]
     */
    public function add()
    {
        $data['step']  = 1;
        $data['users'] = $this->user_m->get_users_all();
        // pp($data);exit();
        $this->load->view('pages/form', $data);
    }

    /**
     * 修改报表
     * @param     $page_id
     * @param int $step
     */
    public function edit($id, $step = 1)
    {
        if ($id && $step) {
            $page            = $this->page_m->get_pages_by_id($id);
            $data['page']    = $page;
            $data['page_id'] = $page['id'];
            $data['users'] = $this->user_m->get_users_all();
            // pp($data);exit();
            $this->load->view('pages/form', $data);
        } else {
            show_message('参数错误', site_url(X_URL . 'page/index'), 1);
        }
    }

    /**
     * 报表信息
     * [update description]
     * @return [type] [description]
     */
    public function update()
    {

        $post    = $this->input->post();
        $page_id = isset($post['page_id']) ? intval($post['page_id']) : false;
        // pp($post);
        // exit;
        $page_title = isset($post['title']) ? htmlspecialchars($post['title'], ENT_QUOTES) : false;
        $user_id    = isset($post['user_id']) ? $post['user_id'] : false;
        if (!$page_id) {
            // 新增操作，先添加用户，再添加报表信息
            $page  = [
                'title'   => $page_title,
                'user_id' => $user_id,
                'content' => $post['content'],
            ];
            $pages = $this->page_m->add_pages($page);
            if ($pages) {
                show_message('新增报表成功', site_url(X_URL . 'page/index'), 1);
            } else {
                show_message('新增报表失败', site_url(X_URL . 'page/edit/'), 1);
            }
        } else {
            // 修改报表信息
            $page          = [
                'title'   => $page_title,
                'user_id' => $user_id,
                'content' => $post['content'],
            ];
            $affected_rows = $this->page_m->save_pages($page_id, $page);
            if ($affected_rows) {
                show_message('修改报表成功', site_url(X_URL . 'page/index'), 1);
            } else {
                show_message('修改报表失败', site_url(X_URL . 'page/edit'), 1);
            }
        }
    }
}
